package biolean.commands;

import java.util.ArrayList;
import java.lang.Class;


import biolean.logic.Pair;


public class CmdRegister
{
	//=====CmdRegister members======

	
	private ArrayList<Pair<Class<?>, String> > mClasses = new ArrayList<Pair<Class<?>,String>>();
	//private List<>
	//=====CmdRegister Constuctors======
	//=====CmdRegister methods======
	
	
	public Class<?> findClass(String name)
	{
		for (Pair<Class<?>, String> pair : mClasses)
		{
			if ( pair.second.equals( name) )
				return pair.first;
		}
		return null;
	}
	
	public String findCommandName(Class<?> cls)
	{
		for (Pair<Class<?>, String> pair : mClasses)
		{
			if ( pair.first.equals( cls) )
				return pair.second;
		}
		return null;
	}
	
	public Class<?> getCommandClass(int index)
	{
		return mClasses.get(index).first;
	}
	
	public String getName(int index)
	{
		return mClasses.get(index).second;
	}
	/**
	 * Prida command do registra commandov
	 * @return - false ked uz sa nachadza, inak true
	 */
	public void register(String name, Class<?> command)
	{
		mClasses.add( new Pair<Class<?>, String> (command, name));
	}
	
	/**
	 * Odstrani command do registra commandov
	 * @return - false ked uz sa nachadza, inak true
	 */
	public void unregister(String name,  Class<?> command)
	{
		Pair<Class<?>, String> toRemove = null;
		for (Pair<Class<?>, String>  pair : mClasses)
		{
			if (pair.first.equals ( command ) && pair.second.equals( name) )
			{
				toRemove = pair;
			}
		}
		if ( toRemove != null)
		{
			mClasses.remove(toRemove);
		}	
	}
	
	public int size()
	{
		return mClasses.size();
	}
	/**
	 * Vrati pole s nazvami zaregistrovanych prikazov 
	 * 
	 * @return nazvy prikazov
	 */
	public String[] getCommandsNames()
	{
		String[] names = new String[mClasses.size()];
		int i = 0;
		for (Pair<Class<?>, String>  pair : mClasses)
		{
			names[i] = pair.second;
			i++;
		}	
		return names;
	}
}
